package managedbean.stakeholder;

import entity.BranchEntity;
import exception.ExistException;
import java.io.IOException;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import javax.servlet.http.HttpSession;
import session.stateless.BranchSessionBean;
import session.stateless.StakeholderSessionBeanStateless;
import util.entityhelper.FnbBranchState;
import util.entityhelper.StakeholderAccountState;

/**
 *
 * @author jamie
 */
@ManagedBean
@ViewScoped
public class StakeholderPreviewManagedBean implements Serializable {

    @EJB
    private StakeholderSessionBeanStateless stakeholderSession;
    
    @EJB
    private BranchSessionBean branchSession;
    
    private Long stakeholderID;
    private String userName;
    private String nextPage;
    private FacesContext ctx = FacesContext.getCurrentInstance();
    private StakeholderAccountState stakeholderAccountState;
    private List<BranchEntity> branches;
    private BranchEntity selectedBranch;
    private boolean success;

    /** Creates a new instance of StakeholderManagedBean */
    public StakeholderPreviewManagedBean() {
    }

    public void initView(PhaseEvent event) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        HttpSession session = (HttpSession) getCtx().getExternalContext().getSession(true);
        System.out.println("retrieving the stakeholder id");
        setStakeholderID((Long) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("stakeholderId"));

        if (getStakeholderID() == null) {

            System.out.println("cannot retrieve stakeholderId... redirecting to login page. ");
            setNextPage(getCtx().getExternalContext().encodeActionURL(getCtx().getApplication().getViewHandler().getActionURL(getCtx(), "/StakeholderLogin.xhtml")));
            try {
                getCtx().getExternalContext().redirect(getNextPage());
            } catch (IOException ioe) {
                ioe.printStackTrace();

            }
        } else {
            if (getStakeholderAccountState() == null) {
                try {
                    System.out.println("Stakeholder ID: " + getStakeholderID());
                    //setStakeholderAccountState(getStakeholderSession().getStakeholderPortalInfoById(getStakeholderID()));
                    //logic change to retrieve previous input and display below. 
                    stakeholderAccountState = (StakeholderAccountState)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("stakeholderAccState");
                    System.out.println("retriving details..." + "userName: " + getUserName() + "imageURL " + getStakeholderAccountState().getImgUrl());
                    
                    if (getStakeholderAccountState().getImgUrl() == null) {
                        getStakeholderAccountState().setImgUrl("/images/upload/in_room_dining_img.jpg");
                    }
                    getStakeholderAccountState().setBranches(getStakeholderSession().viewBranchOutlet(getStakeholderID()));
                    System.out.println("stakeholder Branch size" + getStakeholderAccountState().getBranches().size());

                   

                } catch (Exception ex) {
                    ex.printStackTrace(); //please replace with logger
                    String statusMessage = "ERROR 404";
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, statusMessage));
                }
            }
        }

    }//end of method initview
    
    
        public void saveStakeholderPortalInfo(ActionEvent event) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        //stakeholderAccountState.setStakeholderDesc(stakeholderDesc);
        System.out.println("stakeholder saved description: " + stakeholderAccountState.getStakeholderDesc());
        String statusMessage = null;

        //stakeholderAccountState.setImgUrl((String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("imageFilePath"));

        System.out.println("what's the path of image " + stakeholderAccountState.getImgUrl());
        //stakeholderAccountState.setImgUrl(bean.getImageFilePath());

        try {
            setSuccess(stakeholderSession.addPortalInfoByInfo(stakeholderAccountState));
            System.out.println("success: " + isSuccess());
            //ctx.getExternalContext().getSessionMap().put("stakeholderAccountState", stakeholderAccountState);
            //System.out.println("Save StakeholderID" + ctx.getExternalContext().getSessionMap().get("staffAccountId"));
            if (isSuccess()) {
                statusMessage = "Stakeholder portal is updated successfully";
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessage, statusMessage));
                

            } else {
                statusMessage = "Stakeholder Portal data not saved.";
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, statusMessage));
            }
            //System.out.println("successful? " + statusMessage);
            // FacesMessage msg = new FacesMessage(statusMessage);
            //FacesContext.getCurrentInstance().addMessage(null, msg);
            //FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessage, ""));

        } catch (ExistException ex) {
            ex.printStackTrace();
        }

    }

    
    
    
     public void view_BranchPage(FnbBranchState selectedBranch) throws IOException {

        FacesContext.getCurrentInstance().getExternalContext().getFlash().clear();
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedFnBBranch", branchSession.viewFnBBranch(selectedBranch.getId()));
        //redirecting to view edit branch page
        FacesContext ctx = FacesContext.getCurrentInstance();
        String nextPage = ctx.getExternalContext().encodeActionURL(ctx.getApplication().getViewHandler().getActionURL(ctx, "/stakeholder/PreviewBranch.xhtml"));
        
        FacesContext.getCurrentInstance().getExternalContext().redirect(nextPage);
        
    }

    
    
    /**
     * @return the stakeholderSession
     */
    public StakeholderSessionBeanStateless getStakeholderSession() {
        return stakeholderSession;
    }

    /**
     * @param stakeholderSession the stakeholderSession to set
     */
    public void setStakeholderSession(StakeholderSessionBeanStateless stakeholderSession) {
        this.stakeholderSession = stakeholderSession;
    }

    /**
     * @return the branchSession
     */
    public BranchSessionBean getBranchSession() {
        return branchSession;
    }

    /**
     * @param branchSession the branchSession to set
     */
    public void setBranchSession(BranchSessionBean branchSession) {
        this.branchSession = branchSession;
    }

    /**
     * @return the stakeholderID
     */
    public Long getStakeholderID() {
        return stakeholderID;
    }

    /**
     * @param stakeholderID the stakeholderID to set
     */
    public void setStakeholderID(Long stakeholderID) {
        this.stakeholderID = stakeholderID;
    }

    /**
     * @return the userName
     */
    public String getUserName() {
        return userName;
    }

    /**
     * @param userName the userName to set
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * @return the nextPage
     */
    public String getNextPage() {
        return nextPage;
    }

    /**
     * @param nextPage the nextPage to set
     */
    public void setNextPage(String nextPage) {
        this.nextPage = nextPage;
    }

    /**
     * @return the ctx
     */
    public FacesContext getCtx() {
        return ctx;
    }

    /**
     * @param ctx the ctx to set
     */
    public void setCtx(FacesContext ctx) {
        this.ctx = ctx;
    }

    /**
     * @return the stakeholderAccountState
     */
    public StakeholderAccountState getStakeholderAccountState() {
        return stakeholderAccountState;
    }

    /**
     * @param stakeholderAccountState the stakeholderAccountState to set
     */
    public void setStakeholderAccountState(StakeholderAccountState stakeholderAccountState) {
        this.stakeholderAccountState = stakeholderAccountState;
    }

    /**
     * @return the branches
     */
    public List<BranchEntity> getBranches() {
        return branches;
    }

    /**
     * @param branches the branches to set
     */
    public void setBranches(List<BranchEntity> branches) {
        this.branches = branches;
    }

    /**
     * @return the selectedBranch
     */
    public BranchEntity getSelectedBranch() {
        return selectedBranch;
    }

    /**
     * @param selectedBranch the selectedBranch to set
     */
    public void setSelectedBranch(BranchEntity selectedBranch) {
        this.selectedBranch = selectedBranch;
    }

    /**
     * @return the success
     */
    public boolean isSuccess() {
        return success;
    }

    /**
     * @param success the success to set
     */
    public void setSuccess(boolean success) {
        this.success = success;
    }

    

}

